package com.crazy.web.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crazy.model.major.dto.UserSession;
import com.crazy.model.major.entity.TUser;
import com.crazy.model.major.service.TUserService;
import com.crazy.web.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/16 13:43
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TUserService tUserService;

    @Override
    public TUser getByUserName(String userName) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TUser.Columns.userName, userName);
        List<TUser> tUserList = tUserService.list(queryWrapper);
        if(tUserList.size()>0) {
            return tUserList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public UserSession getUserSession(long userId) {
        TUser user = tUserService.getById(userId);
        UserSession userSession = new UserSession();
        userSession.setId(user.getId());
        userSession.setUserName(user.getUserName());
        userSession.setNickName(user.getNickName());
        userSession.setEmail(user.getEmail());
        userSession.setLocked(user.getLocked());
        return userSession;
    }
}
